const multer = require('multer');
const path = require('path');
const { getFileName } = require('../utils/filename')
// 配置multer存储
const storage = multer.diskStorage({
 destination: function(req, file, cb) {
    cb(null,  'public/uploads/'); // 文件保存的目录
 },
 filename: function(req, file, cb) {
    cb(null, getFileName(file.fieldname) + path.extname(file.originalname)); // 重写文件名
 }
});


const upload = multer({ 
    storage: storage, 
    limits: {fileSize: 1024 * 1024 * 20}, 
    fileFilter: (req, file, cb) => {
    if (file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png') {
        return cb(new Error('错误的文件格式'), false);
    }
    return cb(null, true)
    } 
});
module.exports = upload


